﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMS_Master.Dto;
using TMS_Master.Entities;
using TMS_Master.Interface;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace TMS_Master.DomainService
{
    public class Paying_UserService: ApplicationService, IPaying_UserService
    {
        #region 注入仓储接口
        private readonly IRepository<Paying_User, Guid> _cargoRepository; //付费用户

        public Paying_UserService(IRepository<Paying_User, Guid> cargoRepository)
        {
            _cargoRepository = cargoRepository;
        }
        #endregion

        #region 修改
        [HttpPost]
        public async Task<Paying_UserDto> EditGoods(Paying_UserUpdateDto Pu)
        {
            try
            {
                var res = ObjectMapper.Map<Paying_UserUpdateDto, Paying_User>(Pu);
                var result = await _cargoRepository.UpdateAsync(res);
                return ObjectMapper.Map<Paying_User, Paying_UserDto>(result);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 反填
        [HttpGet]
        public async Task<Paying_UserDto> FullGoods(Guid Id)
        {
            try
            {
                var query = await _cargoRepository.GetAsync(Id);
                return ObjectMapper.Map<Paying_User, Paying_UserDto>(query);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 删除
        [HttpPost]
        public async Task<int> DelCargo(Guid Id)
        {
            try
            {
                await _cargoRepository.DeleteAsync(Id);
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 详情
        [HttpGet]
        public async Task<Paying_UserDto> Details(Guid Id)
        {
            try
            {
                var query = await _cargoRepository.GetAsync(Id);
                return ObjectMapper.Map<Paying_User, Paying_UserDto>(query);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 提交
        [HttpPost]
        public Task<Paying_UserDto> ProvRefer(Guid Id)
        {
            try
            {
                var res = _cargoRepository.FindAsync(Id);
                res.Result.IsState = (TmsEnum.Status)2;
                return (Task<Paying_UserDto>)UnitOfWorkManager.Current.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 重新添加
        [HttpPost]
        public Task<Paying_UserDto> ProvAnew(Guid Id)
        {
            try
            {
                var res = _cargoRepository.FindAsync(Id);
                res.Result.IsState = (TmsEnum.Status)2;
                return (Task<Paying_UserDto>)UnitOfWorkManager.Current.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 显示
        [HttpGet]
        public async Task<PagedResultDto<Paying_UserDto>> GetList(int PageIndex=1, int PageSize=4, string name="", string payname = "", string state = "", string pname = "", string date = "")
        {
            try
            {
                var linq = await _cargoRepository.GetListAsync();

                if (!string.IsNullOrEmpty(name))
                {
                    linq = linq.Where(s => s.Shipper.Contains(name)).ToList();
                }
                if (!string.IsNullOrEmpty(payname))
                {
                    linq = linq.Where(s => s.PayName.Equals(payname)).ToList();
                }
                if (!string.IsNullOrEmpty(state))
                {
                    linq = linq.Where(s => s.IsState.Equals(state)).ToList();
                }
                if (!string.IsNullOrEmpty(pname))
                {
                    linq = linq.Where(s => s.Approver.Contains(pname)).ToList();
                }
                if (!string.IsNullOrEmpty(date))
                {
                    linq = linq.Where(s => s.BusinDate.Equals(date)).ToList();
                }
                //总条数
                int TotalCount = linq.Count;

                var result = linq.Skip((PageIndex - 1) * PageSize).Take(PageSize);

                var res = ObjectMapper.Map<List<Paying_User>, List<Paying_UserDto>>(result.ToList());

                return new PagedResultDto<Paying_UserDto>(TotalCount, res);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 添加
        [HttpPost]
        public async Task<int> CreateGoods(Paying_UserUpdateDto Pu)
        {
            try
            {
                var query = ObjectMapper.Map<Paying_UserUpdateDto, Paying_User>(Pu);
                await _cargoRepository.InsertAsync(query);
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
            
        }

        #endregion
    }
}
